So
far we have discussed how to inherit from an existing class. However, what if
we do not want anybody to inherit from our custom class? We can restrict other
developers from extending our classes with the help of the final keyword.
The final keyword can be applied to:
· A class
· A method
· A variable.
In
general, the use of the final keyword restricts further extensions or
modifications.
The final classes:
In some situations we would not want other developers to extend the classes we have created. For example, in the Java libraries, the String class cannot be extended. This is done to provide better memory management. Strings are immutable objects, which mean their contents cannot be modified once they are declared.
Now, consider the following two declarations:
String
s1 = "abc";
String s2 = "abc";
Both s1 and s2 refer to the same immutable string. Therefore, it makes little sense maintaining two copies of the same object. The JVM creates all string literal objects in a dedicated memory pool and allocates the same reference to all the strings having the same value. Therefore, making the String class final makes sense so that Java’s creators have ultimate control over the memory allocation used by the String type variables. Allowing developers to subclass String may result in independent strings having the same contents.
To
make a class “final,” we use the final keyword as a prefix in the class
declaration, As follows:
public
final class MyClass {
...;
}
Now,
if you attempt to declare another class that extends the final class, as shown
next, the compiler will generate an error during compilation:
public
class YourClass extends MyClass {
...;
}
Declaring
our classes final ensures that others cannot extend them. In our assets management
software, we may want to make the SavingsAccount and CheckingAccount classes final
if we are sure there is no need for anybody to extend them further.
Final Variables
A
variable declared with the final keyword is treated as a constant. Any attempt
to change its value in the program causes a compile-time error. The following
code snippet shows a declaration of a final variable:
public class Math {
public final double PI = 3.14;
...
}
The
variable PI is declared as final. It is assigned a value of 3.14. The value
assigned to PI remains constant throughout the program and cannot be altered by
any further program statement.
By
Java’s coding conventions, all final variables should appear in uppercase
letters with components separated by underscore characters.
A
final variable need not be initialized at the time of declaration. The program
may initialize the variable elsewhere after its declaration. However, such
initialization must be performed only once. A final variable that is not
initialized at the time of its declaration is called a blank final variable.
A
blank
final variable provides much more flexibility in the use of the final
keyword. For example, final fields inside a class can take a different value
for each object and yet retain its immutable quality.
The following code snippet illustrates this:
public class Student {
public final int ID;
public Student() {
ID = createID();
}
public int createID() {
return ... // generate new ID
}
...
}
Leave Comment
1 Comments